package model

import (
	"github.com/gin-gonic/gin"
)

type Examinee struct {
	ENo             string `gorm:"column:e_no"`
	EName           string `gorm:"column:e_name"`
	ESex            string `gorm:"column:e_sex"`
	EAge            string `gorm:"column:e_age"`
	EPolitical      string `gorm:"column:e_political"`
	IsCurrent       string `gorm:"column:is_current"`
	EQualifications string `gorm:"column:e_qualifications"`
	ESource         string `gorm:"column:e_source"`
	ERfeMajor       string `gorm:"column:e_rfe_major"`
	ERfeCategory    string `gorm:"column:e_rfe_category"`
	IsDel           string `gorm:"column:is_del"`
}

func GetExaminee(examineesparam map[string]interface{}) (error, []Examinee, int64) {
	var examineedata []Examinee
	page := examineesparam["page"].(int)
	pageSize := examineesparam["limit"].(int)
	searchText := examineesparam["searchText"].(string)
	var total int64
	err := DB.Table("examinees").Where("e_no like ? and is_del = false", "%"+searchText+"%").Count(&total).Offset((page - 1) * pageSize).Limit(pageSize).Find(&examineedata).Error
	return err, examineedata, total
}

func UpdateExaminee(c *gin.Context) (error, Examinee) {
	var examineedata Examinee
	err := DB.Table("examinees").Where("e_no = ?", examineedata.ENo).Updates(&examineedata).Error
	return err, examineedata
}

func DeletExaminee(c *gin.Context) error {
	ENo := c.Params.ByName("ENo")
	err := DB.Table("examinees").Where("e_no = ?", ENo).Update("is_del", true).Error
	return err
}

func InsertExaminee(c *gin.Context) (error, Examinee) {
	var examineedata Examinee
	c.BindJSON(&examineedata)
	examineedata.IsDel = "false"
	err := DB.Table("examinees").Select("e_no", "e_name", "e_sex", "e_age", "e_political", "is_current", "e_qualifications", "e_source", "e_rfe_major", "e_rfe_category", "is_del").Create(&examineedata).Error
	return err, examineedata
}
